import 'package:built_collection/built_collection.dart';
import 'package:backend.core/core/domain/entities/api/web/api_web_purchased_subscription.dart';
import 'package:backend.core/core/domain/entities/api/web/api_web_purchased_subscription_transaction.dart';
import 'package:backend.core/core/domain/entities/web/web_purchased_subscription_info.dart';
import 'package:backend.core/core/domain/entities/web/web_purchased_subscription_transaction.dart';
import 'package:backend.core/core/domain/entities/web/web_subscription_duration.dart';
import 'package:backend.purchases/domain/helpers/api/web/create_api_web_purchased_subscription_status_from_transactions.dart';
import 'package:backend.purchases/domain/helpers/api/web/create_api_web_purchased_subscription_transactions_from_internal_ones.dart';

abstract class CreateApiWebPurchasedSubscriptionFromInternalOne {
  ApiWebPurchasedSubscription call({
    required BuiltList<WebPurchasedSubscriptionTransaction> transactions,
    required WebPurchasedSubscriptionInfo purchasedSubscriptionInfo,
    required WebSubscriptionDuration subscriptionDuration,
  });
}

class CreateApiWebPurchasedSubscriptionFromInternalOneImpl
    implements CreateApiWebPurchasedSubscriptionFromInternalOne {
  const CreateApiWebPurchasedSubscriptionFromInternalOneImpl({
    required CreateApiWebPurchasedSubscriptionStatusFromTransactions
        createApiWebPurchasedSubscriptionStatusFromTransactions,
    required CreateApiWebPurchasedSubscriptionTransactionsFromInternalOnes
        createApiWebPurchasedSubscriptionTransactionsFromInternalOnes,
  })  : _createApiWebPurchasedSubscriptionStatusFromTransactions =
            createApiWebPurchasedSubscriptionStatusFromTransactions,
        _createApiWebPurchasedSubscriptionTransactionsFromInternalOnes =
            createApiWebPurchasedSubscriptionTransactionsFromInternalOnes;

  final CreateApiWebPurchasedSubscriptionStatusFromTransactions
      _createApiWebPurchasedSubscriptionStatusFromTransactions;
  final CreateApiWebPurchasedSubscriptionTransactionsFromInternalOnes
      _createApiWebPurchasedSubscriptionTransactionsFromInternalOnes;

  @override
  ApiWebPurchasedSubscription call({
    required BuiltList<WebPurchasedSubscriptionTransaction> transactions,
    required WebPurchasedSubscriptionInfo purchasedSubscriptionInfo,
    required WebSubscriptionDuration subscriptionDuration,
  }) {
    final sortedTransactions = transactions.rebuild((transactions) {
      transactions.sort((first, second) {
        return first.time.compareTo(second.time);
      });
    });

    final apiTransactions = _createApiWebPurchasedSubscriptionTransactionsFromInternalOnes(
      transactions: sortedTransactions,
      subscriptionDuration: subscriptionDuration,
      test: purchasedSubscriptionInfo.test,
    );

    final status = _createApiWebPurchasedSubscriptionStatusFromTransactions(
      transactions: apiTransactions,
    );

    final unrevokedTransactions = apiTransactions.where((transaction) {
      return !transaction.revoked;
    });

    ApiWebPurchasedSubscriptionTransaction? lastUnrevokedTransaction;

    if (unrevokedTransactions.isNotEmpty) {
      lastUnrevokedTransaction = unrevokedTransactions.last;
    }

    return ApiWebPurchasedSubscription(
      purchaseItemSku: purchasedSubscriptionInfo.purchaseItemSku,
      autoRenewalEnabled: purchasedSubscriptionInfo.autoRenewalEnabled,
      status: status,
      expirationTime: lastUnrevokedTransaction?.expirationTime,
      test: purchasedSubscriptionInfo.test,
      transactions: apiTransactions,
      originalTransactionId: purchasedSubscriptionInfo.originalTransactionId,
    );
  }
}
